<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Route;
use App\Models\RoutePoint;
use App\Models\Point;


class RouteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        $query = Route::query();
        if($request->keyword) {
            $where['keyword'] = trim($request->keyword);
            $query->where('code','like','%'.$where['keyword'].'%');
        }
        $list = $query->paginate(10);
        return view('admin.route.index',compact('list','where'));
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $points  = Point::all();        
        return view('admin.route.add',compact('points'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token','point_id']);        
        
        $flag = Route::create($data);
        $total_time = 0;
        if($request->point_id) {
            $point_ids = $request->point_id;
            foreach ($point_ids as $k => $v) {
                $rp['route_id'] = $flag->id;
                $rp['point_id'] = $v;
                $point = Point::find($v);
                $total_time += $point->duration;                
                RoutePoint::create($rp);
            }
        }
        $flag->total_time = $total_time;
        $flag->save();
      
        return redirect('zadmin/routes');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       
        $data      = Route::find($id);
        $points    = Point::all();
        $point_ids = RoutePoint::where('route_id',$id)->pluck('point_id')->toArray();
        return view('admin.route.edit',compact('points','data','point_ids'));
    }

    
    public function update(Request $request, $id)
    {
        $data   = $request->except(['_token','_method','point_id']);
        

        $total_time = 0;
        if($request->point_id) {
            RoutePoint::where('route_id',$id)->delete();
            $point_ids = $request->point_id;
            foreach ($point_ids as $k => $v) {
                $rp['route_id'] = $id;
                $rp['point_id'] = $v;
                $point = Point::find($v); 
                $total_time += $point->duration; 
                RoutePoint::create($rp);
            }
        }
        $data['total_time'] = $total_time;
        $flag   = Route::where(['id'=>$id])->update($data);
        return redirect('zadmin/routes');
    }

    
    public function destroy($id)
    {
        $flag = Route::destroy($id);
        return back();
    }

   
}
